// 名字的session
function sessionname(){
    $.ajax({
        url : '/session_name.do',
        type : 'get',
        success : function(data) {
            if(data==="未登陆账号，请先登录。。。。"){
                //layer.alert(data)
            }else{
                $("#session_name").replaceWith('<i class="layui-icon layui-icon-username" style="font-size: 15px;">'+data+'</i>');
            }
        }
    });
}

//注销登录的api
function yorn() {
    var item = window.localStorage.getItem('token');
layer.confirm('是否确认注销登录？', {
    title :'注销',
    btn: ['确认','取消'] //按钮
}, function(index,layero){
    $.ajax({
        async:true,
        url : '/logout.do',
        dataType:'text',
        type:'get',
        beforeSend: function (xhr) {
            xhr.setRequestHeader("token",item);
            layer.msg("正在注销。。。。。")
        },
        success : function(data) {
            if(data === "success"){
                layer.msg(("注销成功，正在跳转。。。。"),{
                    icon: 6,
                    time:2000,
                    end:function () {
                        location.href = "logintest.htm"
                    }
                });
            }else if (data === "fail"){
                layer.msg("注销失败，请刷新页面。重新注销")
            }
        },
        error: function (data) {
            console.info("error: " + data.responseText);
        }
    });
    }, function(){
        layer.close(index);
    });
}

// 生成验证码
function createCode(codeID) {
    var code = "";
    // 验证码长度
    var codeLength = 4;
    // 验证码dom元素
    var checkCode = $(codeID);
    // 验证码随机数
    var random = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
        'S','T','U','V','W','X','Y','Z'];
    for (var i = 0;i < codeLength; i++){
        // 随机数索引
        var index = Math.floor(Math.random()*36);
        code += random[index];
    }
    // 将生成的随机验证码赋值
    checkCode.val(code);
}
// 校验验证码、用户名、密码
function validateCode(inputID,codeID) {
    var inputCode = $(inputID).val().toUpperCase();
    var cardCode = $(codeID).val();
    var loginUsername = $('#loginUsername').val();
    var loginPassword = $('#loginPassword').val();
    if ($.trim(loginUsername) == '' || $.trim(loginUsername).length<=0){
        layer.alert("用户名不能为空");
        return false;
    }
    if ($.trim(loginPassword) == '' || $.trim(loginPassword).length<=0){
        layer.alert("密码不能为空");
        return false;
    }
    if (inputCode.length<=0){
        layer.alert("验证码不能为空");
        return false;
    }
    if (inputCode != cardCode){
        layer.alert("请输入正确验证码");
        return false;
    }
    return true;
}
// 登录流程
function login() {
    var item = window.localStorage.getItem('token');
    if (!validateCode('#loginCard','#loginCode')){
        //阻断提示
    }else {
        var loginUsername = $('#loginUsername').val();
        var loginPassword = $('#loginPassword').val();
        var params = {};
        params.loginUsername = loginUsername;
        params.loginPassword = loginPassword;
        var loginLoadIndex = layer.load(2);
        $('#loginBtn').val("正在登录...");
        $.ajax({
            type:'post',
            url:'/login.do',
            data:$('#lin').serialize(),
            dataType:'text',
            //data:JSON.stringify(params),
            //contentType:'application/json',
            beforeSend:function (xhr) {
                xhr.setRequestHeader("token",item);
            },
            success:function (data) {
                layer.close(loginLoadIndex);
                //var jsonData = JSON.parse(data);
                if (data === "fail"){
                    layer.alert("手机号或者密码错误！！")
                    return false;
                }else if(data==="此用户已经登录，请先返回首页注销！"){
                    layer.msg(("此用户已经登录，请先返回首页注销！自动跳转中。。。。。。。。。。请等待"),{
                        icon: 1,
                        time:3000,
                        end:function () {
                            location.href = "index.htm"
                        }
                    });
                }else{
                    console.log("判断完成！！！");
                    window.localStorage.setItem('token', data)
                    location.href = "/index.htm";
                }
            },
            error:function () {
                layer.close(loginLoadIndex);
                $('#loginBtn').val("登录");
            }
        });
    }

}
// 注册流程
function register() {
    layer.open({
        type:'1',
        content:$('.registerPage'),
        title:'注册',
        area:['430px','400px'],
        btn:['注册','重置','取消',],
        closeBtn:'1',
        btn1:function (index,layero) {
            //注册回调
            layer.close(index);
            var registerUsername = $('#registerUsername').val();
            var registerPassword = $('#registerPassword').val();
            var registerWellPassword = $('#registerWellPassword').val();
            var params = {};
            params.registerUsername = registerUsername;
            params.registerPassword = registerPassword;
            params.registerWellPassword = registerWellPassword;
            params.selectValue = selectValue;
            var registerLoadIndex = layer.load(2);
            $.ajax({
                type:'post',
                url:window.location.protocol+'//'+window.location.host+'/security-web/register.do',
                dataType:'json',
                data:JSON.stringify(params),
                contentType:'application/json',
                success:function (data) {
                    layer.close(registerLoadIndex);
                    layer.msg(data);
                },
                error:function () {
                    layer.close(registerLoadIndex);
                    layer.alert("请求超时！")
                }
            });
        },
        btn2:function (index,layero) {
            //重置回调
            var registerUsername = $('#registerUsername').val("");
            var registerPassword = $('#registerPassword').val("");
            var registerWellPassword = $('#registerWellPassword').val("");
            // 防止注册页面关闭
            return false;
        },
        btn3:function (index,layero) {
            //取消回调
        }
    })
}

function bookimgurl1() {
    $.ajax({
        url:'imgurl.do',
        dataType:'json',
        data:'get',
        success : function(data) {
            for(var i in data){
                var ls = data[i];
                $("#bookimgurl1").append("<div class=\"list-item\" style=\"float:left; margin-left:10px\">"+
                    '<img class="one" id="img'+i+'"  src="'+"http://"+ls.url+'"  style="height: 150px;width: 150px;"/>' +
                    "<p align='center' style='font-weight: 700; max-width:150px'>"+ls.imgname+"</p>" +
                    "<p align='center' style='font-weight: 700; max-width:150px;white-space:nowrap; text-overflow:ellipsis; overflow:hidden;'>"+ls.imginfo+"</p>" +
                    "<p align='center' style='font-weight: 700; max-width:150px'>$100.00</p>" +
                    "</div>")
            }
        }
    })
}
//菜单页
function menu() {
    var data = [
        ["小学图书/小学教辅",
            ["语文",
                ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "小学通用", "小学升初中"],
            ],
            ["数学",
                ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "小学通用", "小学升初中"],
            ],
            ["英语",
                ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "小学通用", "小学升初中"],
            ],
            ["其他科目",
                ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "小学通用", "小学升初中"],
            ]
        ],
        ["初中教辅",
            ["语文",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ],
            ["数学",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ],
            ["英语",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ],
            ["历史",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ],
            ["思想品德",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ],
            ["生物",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ],
            ["地理",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ],
            ["物理",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ],
            ["化学",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ],
            ["其他科目",
                ["七年级", "八年级", "九年级", "初中通用", "中考"],
            ]
        ],
        ["高中教辅",
            ["语文",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["数学",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["英语",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["物理",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["化学",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["生物",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["历史",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["地理",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["政治",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["跨科目",
                ["高中一年级", "高中二年级", "高中三年级", "高中通用", "高考"],
            ],
            ["高考其他",
                ["理科综合", "文科综合", "高考作文", "艺考"],
            ]
        ],
        ["中小学阅读",
            ["一年级",
                ["和大人一起读","读读童谣和儿歌"]
            ],
            ["二年级",
                ["孤独的小螃蟹","小狗的小房子","“歪脑袋”木头桩","小鲤鱼跳龙门","一只想飞的猫","神笔马良","七色花","一起长大的玩具","野葡萄愿望的实现"]
            ],
            ["三年级",
                ["安徒生童话","格林童话","稻草人","中国古代寓言","伊索寓言","克雷洛夫寓言"]
            ],
            ["四年级",
                ["中国古代神话","山海经","希腊神话故事"]
            ],
            ["五年级",
                ["中国民间故事","一千零一夜","列那狐的故事"]
            ],
            ["六年级",
                ["小英雄雨来","爱的教育","童年"]
            ],
            ["七年级",
                ["朝花夕拾","西游记","白洋淀纪事","猎人笔记","镜花缘","湘行散记","骆驼祥子","海底两万里","红岩","创业史","基地","哈利波特与死亡圣器"]
            ],
            ["八年级",
                ["红星照耀中国","昆虫记","飞向太空港","星星离我们有","寂静的春天","傅雷家书","钢铁是怎样炼成的","苏菲的世界","给青年的十二封信","平凡的世界","名人传"]
            ],
            ["九年级",
                ["艾青诗选","水浒传","泰戈尔诗选","唐诗三百首","世说新语","聊斋志异","儒林外史","简爱","围城","格列佛游记","契诃夫短篇小说选","我是猫"]
            ],
            ["高中生必读",
                ["大卫科波菲尔","家","巴黎圣母院","红楼梦","高老头","莎士比亚戏剧集","谈美","三国演义","堂吉诃德"]
            ]
        ],
        ["工具书",
            ["工具书",
                ["专项工具书","字典工具"]
            ]
        ],
        ["英语专项",
            ["英语学习",
                ["英语词汇","英语语法","阅读/完形","口语/听力","英语书面表达"]
            ]
        ],
        ["语文作文",
            ["语文作文",
                ["小学适用","初中适用","高中适用"]
            ]
        ]
    ];

    $('.sort ul li').mouseenter(function() {
        $('.sort ul li').css({"color":"white","background-color":"#f0f0f0"})
        $(this).css({"color":"#f0f0f0","background-color":"white"});
        $('.sort_more').css("display", "inherit");

        $('.sort_more').empty();
        for (var i = 0; i < data.length; i++) { //遍历一级分类
            if ($(this).find('a').text() == data[i][0]) {
                for (var j = 1; j < data[i].length; j++) { //遍历二级分类
                    $(".sort_more").append("<a  href='showbook?value=" + data[i][j][0] + "' class='a_big'>" + data[i][j][0] + "</a>");
                    for (var k = 0; k < data[i][j][1].length; k++) { //遍历三级分类
                        $(".sort_more").append("<a href='showbook?value=" + data[i][j][1][k] + "' class='a_small'>" + data[i][j][1][k] + "</a>");
                    }
                    $(".sort_more").append("<br/><br/>");
                }
            }
        }
    });

    $('.main').mouseleave(function(){
        $('.sort_more').css("display","none");
        $('.sort ul li').css({"color":"white","background-color":"#f0f0f0"})
    });
}


